//
//  PingLunTableViewCell.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/9/2.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "PingLunTableViewCell.h"

@implementation PingLunTableViewCell
{
   // float cellHeight;//cell高度
    UIImageView *_headerImage;//头像
    UILabel * _name,*_fen;//名字/打分
    UIImageView * _xingNum;//等级
    UILabel *_date;//时间
    UILabel *_colorLab;//颜色
    UILabel *_content;//内容
    UIView *_message_images;//图片容器
    NSMutableArray * _images;//图片
    UIButton * _zanBtn;//点赞按钮
  }
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.frame = CGRectMake(0, 0, KScreenWidth, _cellHeight);
        [self layout];
    }
    return self;
}
-(void)layout{
    //头像
    _headerImage = [[UIImageView alloc]init];
    _headerImage.layer.cornerRadius = 45/2;
    _headerImage.layer.masksToBounds =YES;
    [self addSubview:_headerImage];
    //名字
    _name =[[UILabel alloc]init];
    _name .font = [UIFont systemFontOfSize:13];
    _name.textColor = [UIColor colorWithHue:0.10 saturation:0.90 brightness:0.97 alpha:1.00];
    [self addSubview:_name];
    //打分
    _fen = [[UILabel alloc]init];
    _fen .font = [UIFont systemFontOfSize:13];
    _fen.text =@"打分";
    [self addSubview:_fen];
    //内容
    _content = [[UILabel alloc]init];
    _content.numberOfLines = 0;//多行
    _content.font = [UIFont systemFontOfSize:13];
    [self addSubview:_content];
    //时间
    _date = [[UILabel alloc]init];
    _date.textColor = [UIColor grayColor];
    _date.font = [UIFont systemFontOfSize:12];
    [self addSubview:_date];
    //颜色
    _colorLab = [[UILabel alloc]init];
    _colorLab.textColor = [UIColor grayColor];
    _colorLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:_colorLab];
    //点赞按钮
    _zanBtn = [[UIButton alloc]init];
    [_zanBtn setTitle:@"赞" forState:UIControlStateNormal];
    [_zanBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _zanBtn .titleLabel.font = [UIFont systemFontOfSize:13];
    [_zanBtn setImage:[UIImage imageNamed:@"xin (1)"] forState:UIControlStateNormal];
    _zanBtn.layer.borderWidth = 1;
    _zanBtn.layer.cornerRadius =12.58;
    _zanBtn.layer.borderColor = [UIColor grayColor].CGColor;
    [_zanBtn addTarget:self action:@selector(zan:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_zanBtn];
    
}
-(void)zan:(UIButton *)button{
    
}
-(void)setDic:(NSDictionary *)dic{
    [_headerImage sd_setImageWithURL:[NSURL URLWithString:dic[@"headerImage"]]];
   
    _headerImage .frame =CGRectMake(5, 15, 45, 45);
    
    _name.text = dic[@"name"];
    _name.frame = CGRectMake(_headerImage.right+10, 15, KScreenWidth-60, 15);
    _fen.frame =CGRectMake(_headerImage.right+10, _name.bottom+8, 30, 15);
    for (int i=0; i<([(NSString *)dic[@"xing"] intValue]); i++) {
        _xingNum = [[UIImageView alloc]initWithFrame:CGRectMake(_fen.right+2+i*10, _name.bottom+9.5, 10, 10)];
        _xingNum.image = [UIImage imageNamed:@"星"];
        [self addSubview: _xingNum];
    }
    //内容
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:dic[@"content"]];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, ((NSString *)dic[@"content"]).length)];
    //获取计算高度
    CGSize contentSize = [dic[@"content"] boundingRectWithSize:CGSizeMake(KScreenWidth-80, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
    _content.frame = CGRectMake(_headerImage.right+10, _xingNum.bottom+10, contentSize.width, contentSize.height);
    _content.attributedText = attributedString;
    //时间
    CGSize dateSize = [dic[@"date"] boundingRectWithSize:CGSizeMake(KScreenWidth, 15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    _date.frame = CGRectMake(_headerImage.right+10,_content.bottom+7, dateSize.width, 15);
    _date.text = dic[@"date"];
    //颜色
    _colorLab.text = dic[@"color"];
    _colorLab.frame = CGRectMake(_date.right+5, _content.bottom+7, KScreenWidth-_date.right+5, 15);
    _dic = dic;
    [self getImages];
    [self layoutImages];
    _zanBtn.frame = CGRectMake(KScreenWidth-70, _message_images.bottom+5, 60, 25);
     _zanBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 15);
    _zanBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 10);
    _cellHeight = _zanBtn.bottom+10;
    
}

- (void)getImages{
    _images = [NSMutableArray array];
    //如果没图片，返回
    if (!_dic[@"image"]) {
        return;
    }
    NSArray * arr =_dic[@"image"];
    for (int i = 0; i < [arr count]; i++) {
        [_images addObject:arr[i]];
    }
}
- (void)layoutImages{
    float height = 0;//collectionView的高度
    if (_images.count == 1||_images.count == 2||_images.count == 3) {
        height = imageWidth + 2 * 5;
    }
    else if (_images.count == 4||_images.count == 5||_images.count == 6){
        height = imageWidth * 2 + 3 * 5;
    }
    else if (_images.count == 7||_images.count == 8||_images.count == 9){
        height = imageWidth * 3 + 4 * 5;
    }
    if (_message_images) {
        [_message_images removeFromSuperview];
    }
    _message_images = [[UIView alloc]initWithFrame:CGRectMake(0, _date.bottom + 5, KScreenWidth, height)];
    _message_images.backgroundColor = [UIColor cyanColor];
    [self addSubview:_message_images];
    
    [self putImages];
}
-(void)putImages{
     int index = 0;
    for (int x=0; x<3; x++) {
        for (int y=0; y<3; y++) {
             if (index == _images.count) {
                return;
            }
            UIImageView * image =[[UIImageView alloc]initWithFrame:CGRectMake(5+y*(imageWidth+5),5+x*(imageWidth+5), imageWidth, imageWidth)];
            image.backgroundColor = [UIColor whiteColor];
            image.contentMode = UIViewContentModeScaleAspectFit;
            [_message_images addSubview:image];
            //图片点击
            image.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleGesture:)];
            [image addGestureRecognizer:tap];
            [image sd_setImageWithURL:_images[index] placeholderImage:[UIImage imageNamed:@"ii"]];
             index++;
        }
    }
  }
//图片放大
- (void)handleGesture:(UITapGestureRecognizer *)gesture
{
    UIImageView *imageView = [UIImageView new];
    imageView.bounds =  ((UIImageView *)gesture.view).bounds;
    imageView.image = ((UIImageView *)gesture.view).image;
    imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *gesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture1:)];
    [imageView addGestureRecognizer:gesture1];
    [self.window addSubview:imageView];
    [UIView animateWithDuration:0.5 animations:^{
        imageView.frame = CGRectMake(0, 20, self.width, KScreenHeight-20);
    }];
}
//图片缩小
- (void)handleGesture1:(UITapGestureRecognizer *)gesture
{
    UIView *view = gesture.view;
    [UIView animateWithDuration:0.5 animations:^{
        view.bounds = CGRectMake(0, 0, imageWidth, imageWidth);
    } completion:^(BOOL finished) {
        [view removeFromSuperview];
    }];
}
@end
